<template>
  <div class="login-page">
    <div class="login-box">
      <div class="login-content">
        <h2>登陆</h2>
        <div class="input-group">
          <div class="input-wrapper">
            <input
              v-model="phone"
              class="custom-input"
              :class="{ 'error': phoneError }"
              placeholder="手机号"
              @input="validatePhone"
            />
            <div class="error-message" v-if="phoneError">请输入正确的手机号</div>
          </div>
          <div class="input-wrapper">
            <div class="password-input-container">
              <input
                v-model="password"
                :type="showPassword ? 'text' : 'password'"
                class="custom-input"
                :class="{ 'error': passwordError }"
                placeholder="密码"
                @input="validatePassword"
              />
              <i
                class="password-toggle"
                :class="showPassword ? 'el-icon-view' : 'el-icon-hide'"
                @click="showPassword = !showPassword"
              ></i>
            </div>
            <div class="message-container">
              <div class="error-message" v-if="passwordError">密码格式不正确</div>
              <div class="password-rule">密码必须包含大小写字母，长度大于8位，只能填写字母和数字</div>
            </div>
          </div>
        </div>
        <div class="action-group">
          <button @click="login" class="confirm-btn" :disabled="!isFormValid">确认</button>
          <div class="links">
            <!-- <span class="forgot-password">忘记密码？</span> -->
            <span class="register-link" @click="toggleForm">注册</span>
          </div>
        </div>
        <div class="guest-access">
          <button @click="guestAccess" class="guest-btn">游客访问</button>
        </div>
      </div>
      <!-- 流光边框 -->
      <div class="border-line top"></div>
      <div class="border-line right"></div>
      <div class="border-line bottom"></div>
      <div class="border-line left"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useStore } from '../stores/userStore'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const phone = ref('')
const password = ref('')
const phoneError = ref(false)
const passwordError = ref(false)
const store = useStore()
const router = useRouter()
const showPassword = ref(false)

// 手机号验证规则：11位数字，1开头，第二位不能是1246
const phoneRegex = /^1[35789][0-9]{9}$/
// 密码验证规则：必须包含大小写字母，长度大于8位，不能包含特殊字符
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,}$/

const validatePhone = () => {
  phoneError.value = !phoneRegex.test(phone.value)
}

const validatePassword = () => {
  passwordError.value = !passwordRegex.test(password.value)
}

const isFormValid = computed(() => {
  return phoneRegex.test(phone.value) && passwordRegex.test(password.value)
})

function toggleForm() {
  router.push('/register')
}

async function login() {
  if (!isFormValid.value) {
    ElMessage({
      message: '请检查输入格式',
      type: 'warning',
      plain: true,
    })
    return
  }

  try {
    const response = await axios.post('http://localhost:5000/login', {
      phone: phone.value,
      password: password.value
    })
    ElMessage({
      message: '登录成功',
      type: 'success',
      plain: true,
    })
    store.setUser(response.data.user)
    store.setToken(response.data.token)
    router.push('/')
  } catch (error) {
    ElMessage({
      message: '用户名或密码错误',
      type: 'error',
      plain: true,
    })
  }
}

function guestAccess() {
  router.push('/')
}
</script>
<style>
body {
  background-color: #121212;
  margin: 0;
  padding: 0;
}
::-webkit-scrollbar{
  display: none;
}
</style>
<style scoped>
.login-page {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-box {
  position: relative;
  width: 400px;
  padding: 40px;
  background: #1f1b24;
  border-radius: 5px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.login-content {
  position: relative;
  z-index: 1;
}

h2 {
  color: #fff;
  text-align: center;
  font-size: 28px;
  margin-bottom: 30px;
}

.input-group {
  margin-bottom: 25px;
}

.input-wrapper {
  position: relative;
  margin-bottom: 20px;
}

.error-message {
  color: #ff4d4f;
  font-size: 12px;
  margin-bottom: 4px;
  position: static;
}

.password-rule {
  color: #888;
  font-size: 12px;
  position: static;
}

.custom-input {
  width: 100%;
  padding: 12px 20px;
  margin-bottom: 15px;
  background: #2a2a2a;
  border: 1px solid #333;
  border-radius: 5px;
  color: #fff;
  font-size: 16px;
  box-sizing: border-box;
  transition: all 0.3s ease;
}

.custom-input::placeholder {
  color: #888;
}

.custom-input:focus {
  outline: none;
  background: #333;
  border-color: #1e88e5;
}

.custom-input.error {
  border-color: #ff4d4f;
  background: rgba(255, 77, 79, 0.1);
}

.confirm-btn {
  width: 100%;
  padding: 12px 20px;
  background: #1e88e5;
  border: none;
  border-radius: 5px;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.confirm-btn:hover {
  background: #1976d2;
  transform: translateY(-1px);
}

.confirm-btn:disabled {
  background: #333;
  color: #666;
  cursor: not-allowed;
  transform: none;
}

.links {
  display: flex;
  justify-content: space-between;
  margin-top: 15px;
}

.forgot-password, .register-link {
  color: #1e88e5;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.forgot-password:hover, .register-link:hover {
  color: #1976d2;
}

/* 流光边框效果 */
.border-line {
  position: absolute;
}

.border-line.top, .border-line.bottom {
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, transparent, transparent, #1e88e5, transparent, transparent);
}

.border-line.left, .border-line.right {
  width: 2px;
  height: 100%;
  background: linear-gradient(180deg, transparent, transparent, #1e88e5, transparent, transparent);
}

.border-line.top {
  top: 0;
  left: 0;
  animation: animate-x 4s linear infinite;
}

.border-line.right {
  top: 0;
  right: 0;
  animation: animate-y 4s linear infinite;
  animation-delay: 1s;
}

.border-line.bottom {
  bottom: 0;
  right: 0;
  animation: animate-x-reverse 4s linear infinite;
  animation-delay: 2s;
}

.border-line.left {
  bottom: 0;
  left: 0;
  animation: animate-y-reverse 4s linear infinite;
  animation-delay: 3s;
}

@keyframes animate-x {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

@keyframes animate-x-reverse {
  0% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(-100%);
  }
}

@keyframes animate-y {
  0% {
    transform: translateY(-100%);
  }
  100% {
    transform: translateY(100%);
  }
}

@keyframes animate-y-reverse {
  0% {
    transform: translateY(100%);
  }
  100% {
    transform: translateY(-100%);
  }
}

.password-input-container {
  position: relative;
  width: 100%;
}

.password-toggle {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: #888;
  font-size: 20px;
  margin-top: -7px;
  transition: all 0.3s ease;
}

.password-toggle:hover {
  color: #1e88e5;
}

.message-container {
  margin-top: 4px;
  min-height: 40px;
}

.guest-access {
  text-align: center;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #333;
}

.guest-btn {
  background: transparent;
  border: 1px solid #1e88e5;
  color: #1e88e5;
  padding: 8px 20px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.guest-btn:hover {
  background: rgba(30, 136, 229, 0.1);
  transform: translateY(-1px);
}
</style>
